<template>
  <div class="mod-config">
    <div>
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList()"
        ><el-form-item>
          <el-input
          v-model="dataForm.editionName"
          placeholder="出版社名称"
          clearable
        ></el-input>
        </el-form-item>
        <el-form-item>
          <el-select
            v-model="dataForm.isClassify"
            clearable
            placeholder="是否分类"
          >
            <el-option label="是" :value="1"> </el-option>
            <el-option label="否" :value="0"> </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList()">查询</el-button>
          <el-button type="primary" @click="addEdition(0)">新增</el-button>
        </el-form-item>
      </el-form>
    </div>
    <el-table
      :data="dataList"
      stripe
      v-loading="dataListLoading"
      style="width: 100%"
    >
      <el-table-column prop="id" label="出版社id" width="180">
      </el-table-column>
      <el-table-column prop="editionName" label="出版社名称" width="180">
      </el-table-column>
      <el-table-column prop="name" label="出版社封面">
        <template slot-scope="scope">
          <el-image :src="scope.row.editionCover"></el-image>
        </template>
      </el-table-column>
      <el-table-column prop="createtime" label="创建时间"> </el-table-column>
      <el-table-column prop="eorder" label="顺序"> </el-table-column>
      <el-table-column
        fixed="right"
        header-align="center"
        align="center"
        width="150"
        label="操作"
      >
        <template slot-scope="scope">
          <el-button
            type="text"
            size="small"
            @click="addEdition(1, scope.row.id)"
            >修改</el-button
          >
          <el-button
            type="text"
            size="small"
            @click="deleteHandle(scope.row.id)"
            >删除</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页条 -->
    <el-pagination
      @size-change="sizeChangeHandle"
      @current-change="currentChangeHandle"
      :current-page="pageIndex"
      :page-sizes="[10, 20, 50, 100]"
      :page-size="pageSize"
      :total="totalPage"
      layout="total, sizes, prev, pager, next, jumper"
    >
    </el-pagination>
    <el-dialog
      :visible.sync="dialogEditionVisible"
      :title="type == 0 ? '添加出版社信息' : '修改出版社信息'"
    >
      <el-form
        :rules="dataRule"
        ref="editionForm"
        :model="edition"
        label-width="120px"
      >
        <el-form-item label="出版社名称" prop="editionName">
          <el-input v-model="edition.editionName" />
        </el-form-item>
        <el-form-item label="出版社封面" prop="editionCover">
          <el-upload
            class="avatar-uploader"
            :action="'http://121.41.167.195:8000/api/oss/upload'"
            ref="editionUpload"
            :show-file-list="false"
            :on-success="handleAvatarSuccess"
            :before-upload="beforeAvatarUpload"
          >
            <img v-if="edition.editionCover" :src="edition.editionCover" />
            <i v-else class="el-icon-plus avatar-uploader-icon"></i>
          </el-upload>
        </el-form-item>
        <el-form-item label="是否分类" prop="isClassify">
          <el-radio v-model="edition.isClassify" :label="1">是</el-radio>
          <el-radio v-model="edition.isClassify" :label="0">否</el-radio>
        </el-form-item>
        <el-form-item label="顺序" prop="eorder">
          <el-input-number
            v-model="edition.eorder"
            controls-position="right"
            :min="1"
          ></el-input-number>
        </el-form-item>
      </el-form>

      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogEditionVisible = false">取 消</el-button>
        <el-button type="primary" @click="saveOrUpdate">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  data() {
    return {
      dataForm: {
        editionName: "", 
        isClassify: null, 
      },
      edition: {
        // 出版社对象
        eorder: 1,
        editionName: "",
        editionCover: "",
        id: null,
        isClassify: 0,
      },
      dataRule: {
        editionName: [
          { required: true, message: "出版社名称不能为空", trigger: "blur" },
        ],
        editionCover: [{ required: true, message: "封面不能为空" }],
      },
      type: 0,
      dialogEditionVisible: false,
      dataList: [],
      pageIndex: 1,
      pageSize: 10,
      totalPage: 0,
      dataListLoading: false,
    };
  },
  activated() {
    this.getDataList();
  },
  methods: {
    saveOrUpdate() {
      if (this.type == 0) {
        // 添加章节
        this.editionFormSubmit();
      } else {
        // 修改章节
        this.updateEdtion();
      }
    },
    // 表单提交
    editionFormSubmit() {
      // 表单验证
      this.$refs["editionForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbookedition/save`),
            method: "post",
            data: this.$http.adornData(this.edition, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "添加成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.dialogEditionVisible = false;
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
              this.saveBtnDisabled = false;
            }
          });
        } else {
          this.saveBtnDisabled = false;
        }
      });
    },
    updateEdtion() {
      // 表单验证
      this.$refs["editionForm"].validate((valid) => {
        if (valid) {
          this.$http({
            url: this.$http.adornUrl(`/sf/cbbookedition/update`),
            method: "post",
            data: this.$http.adornData(this.edition, false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "修改成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.dialogEditionVisible = false;
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        } else {
          this.saveBtnDisabled = false;
        }
      });
    },
    //上传成功后的回调函数
    handleAvatarSuccess(response) {
      this.edition.editionCover = response.url;
    },
    beforeAvatarUpload() {},
    addEdition(status, id) {
      // 重置表单 应该只是把表单有的清空而已
      this.$nextTick(() => {
        this.$refs.editionForm.resetFields();
      });
      if (status == 0) {
        // 添加
        this.type = 0;
        // 清空
        this.edition.id = null;
        this.edition.isClassify = 0;
      } else {
        // 修改
        this.type = 1;
        // 回显数据
        this.$http({
          url: this.$http.adornUrl("/sf/cbbookedition/info/" + id),
          method: "get",
        }).then(({ data }) => {
          this.edition = data.edition;
        });
      }
      this.dialogEditionVisible = true;
    },
    // 获取校区分页数据
    getDataList() {
      this.dataListLoading = true;
      this.$http({
        url: this.$http.adornUrl("/sf/cbbookedition/list"),
        method: "get",
        params: this.$http.adornParams({
          page: this.pageIndex,
          limit: this.pageSize,
          editionName: this.dataForm.editionName, 
          isClassify: this.dataForm.isClassify, 
        }),
      }).then(({ data }) => {
        if (data && data.code === 0) {
          console.log(data);
          this.dataList = data.bookEditionPage.records;
          this.totalPage = data.bookEditionPage.total;
        } else {
          this.dataList = [];
          this.totalPage = 0;
        }
        this.dataListLoading = false;
      });
    },
    // 每页数
    sizeChangeHandle(val) {
      this.pageSize = val;
      this.pageIndex = 1;
      this.getDataList();
    },
    // 当前页
    currentChangeHandle(val) {
      this.pageIndex = val;
      this.getDataList();
    },
    // 新增或修改
    addOrUpdateHandle(editionId) {
      this.$router.push({
        path: "/ruanbi/add/" + editionId,
      });
    },
    // 删除
    deleteHandle(editionId) {
      this.$confirm(`确定对[出版社id=${editionId}]进行[删除]操作?`, "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          this.$http({
            url: this.$http.adornUrl("/sf/cbbookedition/delete"),
            method: "post",
            data: this.$http.adornData([editionId], false),
          }).then(({ data }) => {
            if (data && data.code === 0) {
              this.$message({
                message: "操作成功",
                type: "success",
                duration: 1500,
                onClose: () => {
                  this.getDataList();
                },
              });
            } else {
              this.$message.error(data.msg);
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>
<style scoped>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}
.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>